//
//  XJYRegisterViewController.m
//  高仿萝卜书摘
//
//  Created by 谢俊逸 on 2016/11/27.
//  Copyright © 2016年 谢俊逸. All rights reserved.
//


//
#import "XJYRegisterViewController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "XJYHeader.h"

//cell
#import "XJYRegisterNameTableViewCell.h"
#import "XJYRegisterPhoneTableViewCell.h"
#import "XJYRegisterPasswordTableViewCell.h"
#import "XJYRegisterPhoneVerificationCodeTableViewCell.h"

@interface XJYRegisterViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) TPKeyboardAvoidingTableView *myTableView;

@property (nonatomic,strong) UIView *bottomView;
@property (nonatomic,strong) UIView *headerView;

@property (nonatomic,strong) FUIButton *registerButton;

@property (nonatomic,strong) UIBarButtonItem *leftBarButtonItem;

@property (nonatomic,strong) NSString *nameString;
@property (nonatomic,strong) NSString *passwordString;


@end

@implementation XJYRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
    self.nameString = @"";
    self.passwordString = @"";
    self.navigationItem.leftBarButtonItem = self.leftBarButtonItem;
    
#pragma mark Notification
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(registerSuccessful) name:@"registerSuccessful" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(registerFailed) name:@"registerFailed" object:nil];
    
    _myTableView = ({
        _myTableView = [[TPKeyboardAvoidingTableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _myTableView.backgroundColor = ColorWithRGBA(235, 235, 235, 1);
        _myTableView.tableHeaderView = self.headerView;
        _myTableView.tableFooterView = self.bottomView;
        [self.view addSubview:_myTableView];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
#pragma mark registerCell
        [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([XJYRegisterNameTableViewCell class]) bundle:nil] forCellReuseIdentifier:kXJYRegisterNameTableViewCell];
        [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([XJYRegisterPhoneTableViewCell class]) bundle:nil] forCellReuseIdentifier:kXJYRegisterPhoneTableViewCell];
        [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([XJYRegisterPasswordTableViewCell class]) bundle:nil] forCellReuseIdentifier:kXJYRegisterPasswordTableViewCell];
        [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([XJYRegisterPhoneVerificationCodeTableViewCell class]) bundle:nil] forCellReuseIdentifier:kXJYRegisterPhoneVerificationCodeTableViewCell];
        
        _myTableView;
    });
}
#pragma mark Lazy loading
- (UIView *)headerView{
    UIView *headerView = [[UIView alloc]initWithFrame:RectMake_LFL(0, 0, 375, 100)];
    headerView.backgroundColor = ColorWithRGBA(235, 235, 235, 1);
    return headerView;
}

- (UIView *)bottomView{
    UIView *bottomView = [[UIView alloc]initWithFrame:RectMake_LFL(0, 0, 275, 300)];
    bottomView.backgroundColor = ColorWithRGBA(235, 235, 235, 1);
    [bottomView addSubview:self.registerButton];
    return bottomView;
}

- (FUIButton *)registerButton{
    FUIButton *registerButton = [[FUIButton alloc]initWithFrame:RectMake_LFL(375/2-160, 30, 320, 44)];
    [registerButton setButtonColor:[UIColor greenSeaColor]];
    registerButton.cornerRadius = 10;
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [registerButton addTarget:self action:@selector(myRegisterButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    return registerButton;
}

- (UIBarButtonItem *)leftBarButtonItem{
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonClicked:)];
    leftBarButtonItem.tintColor = [UIColor greenSeaColor];
    return leftBarButtonItem;
}


#pragma mark UITableViewDataSource 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    __weak typeof(self) weakSelf = self;

    if (indexPath.row == 0) {
        XJYRegisterNameTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kXJYRegisterNameTableViewCell forIndexPath:indexPath];
        cell.textValueChangedBlock = ^(NSString *valueStr){
            weakSelf.nameString = valueStr;
        };
        return cell;
    }else if (indexPath.row == 1){
        XJYRegisterPasswordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kXJYRegisterPasswordTableViewCell forIndexPath:indexPath];
        cell.textValueChangedBlock = ^(NSString *valueStr){
            weakSelf.passwordString = valueStr;
        };
        return cell;
    }
    XJYRegisterNameTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kXJYRegisterNameTableViewCell forIndexPath:indexPath];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark ButtonClicked

- (void)cancelButtonClicked:(UIBarButtonItem *)leftBarButton{
    NSLog(@"cancel button clicked");
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)myRegisterButtonClicked:(UIButton *)button{
    
    [[NSNotificationCenter defaultCenter]postNotification:[NSNotification notificationWithName:@"resignRegisterPasswordCellKeyboradFirstResponder" object:nil]];
    
    NSLog(@"注册");
    if ([self.nameString isEqualToString:@""]) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = NSLocalizedString(@"用户名不能为空", @"HUD message title");
        hud.yOffset = 100;;
        hud.xOffset = 0.f;
        [hud hide:YES afterDelay:1.f];
    }else if ([self.passwordString isEqualToString:@""]){
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = NSLocalizedString(@"密码不能为空", @"HUD message title");
        hud.yOffset = 100;;
        hud.xOffset = 0.f;
        [hud hide:YES afterDelay:1.f];
    }else{
        [[XJYNetApiClient shareXJYNetClient] registerUser:@{
                                                           @"name":self.nameString,
                                                           @"password":self.passwordString
                                                           }];
    }
}


#pragma mark Notification

- (void)registerSuccessful{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = NSLocalizedString(@"恭喜注册成功", @"HUD message title");
    hud.yOffset = 100;;
    hud.xOffset = 0.f;
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
        //转会菊花 给用户提示
        sleep(1.);
        dispatch_async(dispatch_get_main_queue(), ^{
            [hud hide:YES afterDelay:1.f];
            [self dismissViewControllerAnimated:YES completion:^{
            }];
        });

    });
    
}

- (void)registerFailed{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = NSLocalizedString(@"注册失败，账号已存在", @"HUD message title");
    hud.yOffset = 100;;
    hud.xOffset = 0.f;
    [hud hide:YES afterDelay:1.0f];
}

@end
